#pragma once
#include "movies_admin.h"
#include "accounts_admin.h"
#include "rental_admin.h"
#include "reservation_admin.h"
#include <conio.h>
#include <sstream>

using namespace std;


int main()
{
	char use_case;
	movies_admin *movies_admin_=movies_admin::get_movies_admin();
	rental_admin *rental_admin_=rental_admin::get_rental_admin();
	accounts_admin *accounts_admin_=accounts_admin::get_accounts_admin();
	reservation_admin *reservation_admin_=reservation_admin::get_reservation_admin();


	cout<<"\t\t\t Welcome to our Movie Rental Store "<<"\n\n\n\n";

	cout<<"Choose one of the following use cases: \n";
	cout<<"'a': To add a new movie\t\t"<<" 'o': To open an account for a new member"<<endl;
	cout<<"'r': To rent a movie\t\t"<<" 'n': To return a movie\t"<<endl;
	cout<<"'v': To make a reservation\t"<<" 'c': To cancel a reservation\t"<<endl;
	cout<<"'u': To update existing account\t"<<" 's': To search for a movie\t"<<endl;
	cout<<" \t\t\t press 'e' to exist the program "<<endl;
	cin>>use_case;
	cout<<endl;

	while(use_case!= 'e') 
	{
		
		switch (use_case)
  {
  case 'a':
   {
    cout<<"\t\t\t Add new movie: \n\n";
    cout<<"Please type the name of the movie: ";
    string name;
	char n[30];
	getchar();
	gets_s(n);
	name = n;
//	fflush(stdin);
//	getline(cin,name);
    if(movies_admin_->search(name))
    {
     cout<<"\n This movie is already existed! , Do You want to add new copies? (press 'y' for yes) ";
     if (_getch()=='y')
     {
      cout<<"\n Please type the number of extra CDs available: ";
      int extra_copies;
      cin>>extra_copies;
      cout<<"\n Confirm? (press 'y' for yes) ";
      if (_getch()=='y')
       {
        movies_admin_->add_copies(name, extra_copies);
        cout<<"\n \t Successfully added .. \n";
        break;
       }
	  cout<<"\n\n \t Adding new copies is CANCELLED! \n";
      break;
	 }
     cout<<"\n\n \t Adding new movie is CANCELLED! \n";
     break;
    }
    cout<<"Number of CDs available: ";
    int copies;
    cin >>copies;
    cout<<"Do you want to write information about it? (press 'y' for Yes)";
    if (_getch()=='y')
    {
     cout<<"\n Write some details: ";
     string details; char d [100]; getchar(); gets_s (d);
   details = d ;
     cout<<"\n Confirm? (press 'y' to confirm)";
     if (_getch()=='y')
     {
     movies_admin_->add_movie(name, copies,details);
     cout<<"\n \t Successfully added .. \n";
     break;
     }
     cout<<"\n\n \t Adding new movie is CANCELLED! \n";
     break;
    }
    cout<<"\n Confirm? (press 'y' to confirm) \n";
     if (_getch()=='y')
     {
     movies_admin_->add_movie(name, copies, "No available details");
     cout<<"\n \t Successfully added .. \n";
     break;
     }
     cout<<"\n\n \t Adding new movie is CANCELLED! \n";
     break;
   }
   break;

  case 'o':
   {
	   cout<<"\t\t\t Open an account: "<<endl;
	   cout<<"Please Enter Mobile Number ";
	   int mobile;
	   cin>>mobile;
	   if(accounts_admin_->search_m(mobile))
	   {
		   cout<<"\n\t\t This member is already existed !\n";
		   break;
	   }
	   cout<<"Please write the following: "<<endl;
	   cout<<"Name: ";
	   string name; char d [100]; getchar(); gets_s (d);
	   name = d ;
	   cout<<"Address: ";
	   string address; getchar(); gets_s (d);
	   address = d ;
	   cout<<"e-mail: ";
	   string email; getchar(); gets_s (d);
	   email = d ;
	   cout<<"Credit Card Number: ";
	   int credit_card;
	   cin>>credit_card;
	   cout<<"\n Confirm? (press 'y' to confirm) \n";
	   if (_getch()=='y')
	   {
	   if (!accounts_admin_->add_m(name,mobile,address,email,credit_card))
	   {
		   cout<<"\n\t\t This member is already existed !\n";
		   break;
	   }
	   cout<<"\n\t Successfully added .. \n";
	   break;
	   }
   }

  case 'r':
   {
    cout<<"\t\t\t Rent Movie: "<<endl;
    cout<<"Please Enter Mobile Number ";
    int mobile;
    cin>>mobile;
    cout<<"Please Enter Movie name ";
    string movie_r;
	char m [100]; getchar(); gets_s (m);
    movie_r = m ;
    if(!rental_admin_->rent(movie_r,mobile))
	{
		cout<<"\n\t\t Sorry, Renting is unavailable ..\n";
		break;
	}
	cout<<"\n\t\tthe rental is successfully created ..\n";
    break;
   }

  case 'n':
   {
	   cout<<"\t\t\t Return a movie: "<<endl;
	   cout<<"Please Enter customer's phone number: ";
	   int phone;
	   cin>>phone;
	   if(!rental_admin_->valid_rental(phone))
	   {
		   cout<<endl<<"There is no valid rental !\n";
		   break;
	   };

	   cout<<"The movie rented is : "<<rental_admin_->get_rental(phone)->get_movie()->get_movie_title()->get_name()<<endl;
	   if(rental_admin_->check_late(rental_admin_->get_rental(phone)))
	   {
		   cout<<"This rental is out of date, there is late fees!"<<endl;
		   cout<<"The late fees amount is: "<<rental_admin_->get_rental(phone)->get_fees()->calculate_late_fees();
	   };
	   cout<<"The money should be paid deposit now is: "<<rental_admin_->calculate_left_fees(rental_admin_->get_rental(phone))<<endl;
	   cout<<"Paid ? press 'y' for yes";
	    if (_getch()=='y')
	   {
		   rental_admin_->return_(phone);
		   cout<<"\n\t\t The movie is successfully returned .. \n";
		   break;
	   }
		cout<<"Take Care : the movie was not returned !\n";
		break;
   }
  case 'v':
   {
	   cout<<"\t\t\t Reserve a movie: "<<endl;
	   cout<<"Please Enter customer's phone number: ";
	   int mobile;
	   cin>>mobile;
	   if(!accounts_admin_->search_m(mobile))
	   {
		   cout<<"\n\t\t Sorry, ONLY members can reserve movies ! \n";
		   break;
	   }
	   cout<<endl<<"Please Enter the movie name: ";
	   string name; char d [100]; getchar(); gets_s (d);
	   name = d ;
	   if(!movies_admin_->search(name)) 
	   {
		   cout<<"Can't make a reservation, the movie is not found !\n";
		   break;
	   }
	   reservation_admin_->reserve(name,mobile);
	   cout<<"\n\t\t The movie is successfully reserved .. \n";
	   break;
   }
  case 'c':
   {
	   cout<<"\t\t\t Cancel a reservation: "<<endl;
	   cout<<"Please Enter customer's phone number: ";
	   int mobile;
	   cin>>mobile;
	   if(!reservation_admin_->is_reserved(mobile))
	   {
		   cout<<"\n\t\t Reservation is not found !\n"; break;
	   }
	   else
	   cout<<"The movie reserved is: "<<reservation_admin_->get_movie(mobile)<<endl;
	   cout<<"Confirm cancellation? (press 'y' for yes"<<endl;
	   if (_getch()=='y')
	   {
		   reservation_admin_->cancel_r(mobile);
		   cout<<"\n\t\t The reservation is cancelled successfully ..\n";
		   break;
	   }
	   break;

   }
  case 'u':
   {
			   cout<<"\t\t\t Update account: "<<endl;
			   cout<<"Please Enter customer's phone number: ";
			   int mobile;
			   cin>>mobile;
			   if(!accounts_admin_->search_m(mobile))
			   {
				   cout<<"\n\t This member is not existed ! \n"<<endl;
				   break;
			   }
			   cout<<"Please write the following : "<<endl;
			   cout<<"new Name: ";
			   string name; char d [100]; getchar(); gets_s (d);
			   name = d ;
			   cout<<"new Address: ";
			   string address; getchar(); gets_s (d);
			   address = d ;
			   cout<<"new e-mail: ";
			   string email; getchar(); gets_s (d);
			   email = d ;
			   cout<<"new Credit Card Number: ";
			   int credit_card;
			   cin>>credit_card;
			   cout<<"\n Confirm? (press 'y' to confirm) \n";
			   if (_getch()=='y')
			   {
				   accounts_admin_->update_account(name,mobile,address,email,credit_card);
				   cout<<"\n\t Successfully updated .. \n";
				   break;
			   }
			   cout<<"\n\t Updating is cancelled ! \n"<<endl;
			   break;
   }
  case 's':
   {
	   cout<<"\t\t\t search: "<<endl;
	   cout<<"Please Enter movies name: ";
	   string name; char d [100]; getchar(); gets_s (d);
	   name = d ;
	   if(movies_admin_->search(name))
	   {
		   cout<<"\n\t The movie is existed .."<<endl;
		   cout<<"copies available: "<<movies_admin_->get_copies(name)<<endl;
		   cout<<"movie info:  ";movies_admin_->view_movie_details(name);cout<<endl;
		   break;
	   }
	   cout<<"\n\t\t The movie is not existed !\n";
		   break;
   }
  default:
   {
    cout<<"\t Unavailable operation! \n";
    break;
   }
  }
  cout<<endl<<"Choose one of the following operations: \n";;
  cout<<"'a': To add a new movie CD\t"<<"'o': To open an account for a new member"<<endl;
  cout<<"'r': To rent a movie\t\t"<<"'n': To return a movie"<<endl;
  cout<<"'v': To make a reservation\t"<<"'c': To cancel a reservation"<<endl;
  cout<<"'u': To update existing account\t"<<"'s': To search for movie"<<endl;
  cout<<" \t\t press 'e' to exist the program "<<endl;
   fflush(stdin);
   cin>>use_case;
  cout<<endl<<endl;
 }
 cout<<"\n \t\t\tThank You for using Movie Rental Store Application\n\n";
 return 0;
}